import uuid from 'uuid';
import query from './atom/db/query';
import update from './atom/db/update';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	screen_no: string; s_menu_order: number; e_menu_order: number; no: string;
}

interface quickMenu extends IBaseTable {
	no: string; screen_no: string; menu_order: number;
}

async function atom(msg: Message) {
	const systemid = '6051';

	const q = query(action_id, session_id, spaceid, systemid);
	q.prepare('quick_menu', ['screen_no', '_id', 'no', 'menu_order'], { screen_no: msg.screen_no, menu_order: { $lte: msg.e_menu_order, $gte: msg.s_menu_order } });
	// 快捷菜单
	const [data] = await q.exec();

	const upd_data: any[] = [];
	data.forEach((d: quickMenu) => {
		const no = d.no;
		if (no === msg.no) {
			d.menu_order = msg.s_menu_order;
		} else {
			d.menu_order = d.menu_order + 1;
		}
		upd_data.push(d);
	});

	const { exec, prepare } = update(action_id, session_id, spaceid, systemid);

	// 快捷菜单
	prepare('quick_menu', upd_data);
	const res = await exec();

	if (res.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}